/*
 * Copyright 2010-2011 Mike Novak <michael.novakjr@gmail.com>
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.codeversed.plugins.buzz.activity;

import android.app.Activity;
import android.app.Dialog;
import android.app.ProgressDialog;
import android.graphics.Bitmap;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.Window;
import android.widget.ImageView;

import com.codeversed.plugins.buzz.R;
import com.codeversed.plugins.buzz.util.ImageUtils;
import com.codeversed.plugins.buzz.widget.ImageListener;

import java.lang.ref.WeakReference;

public class PhotoViewActivity extends Activity implements ImageListener {
    public static final int DIALOG_LOADING = 0x1;
    
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        ImageView view = new ImageView(this);
        view.setScaleType(ImageView.ScaleType.CENTER_INSIDE);

        setContentView(view);
        showDialog(DIALOG_LOADING);

        PhotoDownloadTask task = new PhotoDownloadTask(view);
        task.setImageListener(this);
        task.execute(getIntent().getDataString());
    }

    @Override
    protected Dialog onCreateDialog(int id, Bundle args) {
        switch (id) {
        case DIALOG_LOADING:
            ProgressDialog dialog = new ProgressDialog(this);
    		dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
    		dialog.setMessage(getString(R.string.loading));
    		return dialog;
        default:
            throw new IllegalArgumentException("Unknown dialog id: " + id);
        }
    }

    public void onImageDisplayed() {
        removeDialog(DIALOG_LOADING);
    }

    static class PhotoDownloadTask extends AsyncTask<String, Void, Bitmap> {
        private final WeakReference<ImageView> mImageViewRef;
        private WeakReference<ImageListener> mListenerRef;

        public PhotoDownloadTask(ImageView view) {
            mImageViewRef = new WeakReference<ImageView>(view);
        }

        public void setImageListener(ImageListener listener) {
            mListenerRef = new WeakReference<ImageListener>(listener);
        }

        @Override
        protected Bitmap doInBackground(String... params) {
            return ImageUtils.downloadBitmap(params[0]);
        }

        @Override
        protected void onPostExecute(Bitmap image) {
            if (isCancelled()) {
                return;
            }

            ImageView imageView = mImageViewRef.get();

            if (imageView != null) {
                imageView.setImageBitmap(image);
            }

            ImageListener listener = mListenerRef.get();

            if (listener != null) {
                listener.onImageDisplayed();
            }
        }
    }
}
